<?php
header('Expires: '.gmdate('D, d M Y H:i:s T', time()+315360000));
header('Cache-Control: max-age=315360000');
header("Content-type: text/html; charset=UTF-8");

include('functions.inc.php');
$link = db_connect();

if (!isset($_GET['what'])) {
	$where = "(state LIKE '%new%' AND state NOT LIKE '%unread%')";
	$what = "new";
} else if ($_GET['what'] == 'unread') {
	$where = "state LIKE '%unread%'";
	$what = "unread";
} else if ($_GET['what'] == 'starred') {
	$where = "state LIKE '%starred%'";
	$what = "starred";
} else if ($_GET['what'] == 'new') {
	$where = "state LIKE '%new%'";
	$what = "new";
} else {
	$where = "(state LIKE '%new%' AND state NOT LIKE '%unread%')";
	$what = "new";
}

if (isset($_GET['sub'])) {
	$where .= " AND subId = " . mysql_real_escape_string($_GET['sub']);
}

if (isset($_GET['id'])) {
	$articles_query = "SELECT a.*, s.title as subTitle FROM articles a LEFT JOIN subscriptions s ON (a.subId = s.id) ORDER BY pubDate ASC";
} else {
	$articles_query = "SELECT a.*, s.title as subTitle FROM articles a LEFT JOIN subscriptions s ON (a.subId = s.id) WHERE $where ORDER BY pubDate ASC LIMIT " . $config['max_articles_per_spider_page'];
}

$result = mysql_query($articles_query, $link)
	or die("Can't load articles: " . mysql_error() . "\n");

$rows = array();
$numRows = 0;
$go = false;
while ($article = mysql_fetch_array($result)) {
	$id = $article['id'];
	if (isset($_GET['id'])) {
		if ($id == $_GET['id']) {
			$go = true;
		}
		if (!$go) {
			continue;
		}
		if ($numRows >= $config['max_articles_per_spider_page']) {
			break;
		}
	}
	if (filtered_article($article['title'],$article['description'])) {
		continue;
	}
	$rows[] = $article;
	$numRows++;
}
?>
<html>
<head>
	<title>GobbleRSS</title>
	<link href="css/gob.css" type="text/css" rel="stylesheet">
	<?php if (isset($isPDF) && $isPDF): ?>
		<link href="css/pdf.css" type="text/css" rel="stylesheet">
	<?php endif; ?>
	<?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE || isset($_GET['iPhone'])): ?>
		<link href="css/iphone.css" type="text/css" rel="stylesheet">
	<?php endif; ?>
	<meta name="author" content="GobbleRSS (http://gobblerss.pommepause.com)" />
	<meta name="subject" content="<?php echo $numRows ?> <?php echo $what ?> RSS articles" />
</head>

<body>
<h1>Reading (<span id="numArticles"><?php echo $numRows ?></span>) <?php echo $what ?> articles</h1>

<div id="allArticles">
<?php
foreach ($rows as $article) {
	$id = $article['id'];
	$title = $article['title'];
	$subTitle = $article['subTitle'];
	$description = $article['description'];
	$alink = $article['link'];
	$date = date("Y-m-d",strtotime($article['pubDate'])+7*60*60);
	$states = explode(',',$article['state']);

	$description = str_replace('’','\'',$description);
	?>
	<div class="heading">
		<a id="currentLink" href="<?php echo $alink ?>" target="_blank">
			<span class="articleTitle"><?php echo $id ?>. <?php echo $title ?></span>
		</a>
		<span class="channel" style="cursor: default"><?php echo $date ?> - <?php echo $subTitle ?></span>
	</div>
	<div class="article"><?php echo $description ?></div>
	<hr />
	<?php

	if (isset($_GET['test'])) {
		continue;
	}

	$newStates = array();
	foreach ($states as $state) {
		if ($state != 'new') {
			$newStates[] = $state;
		}
	}
	$states = implode($newStates,',');

	$update_query = sprintf("UPDATE articles SET state = %s WHERE id = %s",
		$states == '' ? "null" : "'$states'",
		$id
	);
	mysql_query($update_query, $link)
		or die("Can't update article state: " . mysql_error() . "\n");
}
mysql_close($link);
?>
</div>
</body>
</html>
